const db = require("../DBHelper");

const jwt = require("jsonwebtoken");

class Users {
  async login(req, res) {
    setTimeout(async () => {
      let sql =
        "select id,username from users where status=1 and username=? and password=?";
      let params = [req.body.username, req.body.password];
      let result = await db.exec(sql, params);
      let isLogin = result && result.length >= 1;
      if (isLogin) {
        let token = jwt.sign(JSON.stringify(result[0]), "cs2209"); //生成token
        res.json({ msg: "登陆成功!", code: 1, data: result[0], token: token });
      } else {
        res.json({ msg: "登陆失败!", code: -1 });
      }
    }, 500);
  }
}

module.exports = new Users();
